package com.example.controller;

import com.example.common.Result;
import com.example.entity.Item;
import com.example.service.ItemService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/item")
public class ItemController {
    @Resource
    private ItemService itemService;

    @GetMapping("/selectPage_id")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "6") Integer pageSize, Item item, @RequestParam Integer businessId) {
        PageInfo<Item> pageInfo = itemService.selectPage(currentPage, pageSize, item, businessId);
        return Result.success(pageInfo);
    }

    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "6") Integer pageSize, Item item) {
        PageInfo<Item> pageInfo = itemService.selectPage(currentPage, pageSize, item);
        return Result.success(pageInfo);
    }
    @GetMapping("/selectPageByCid")
    public Result selectPageByCid(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "6") Integer pageSize, Item item, @RequestParam Integer cid) {
        PageInfo<Item> pageInfo = itemService.selectPageByCid(currentPage, pageSize, item,cid);
        return Result.success(pageInfo);
    }
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Item item=itemService.selectById(id);
        return Result.success(item);
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody Item item) {
        System.out.println(item);
        itemService.addItem(item);
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Item item) {
        itemService.updateItem(item);
        return Result.success();
    }

}
